package com.he.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.he.baseservice.exceptionhandler.EduException;
import com.he.eduservice.entity.EduChapter;
import com.he.eduservice.entity.EduVideo;
import com.he.eduservice.entity.vo.ChapterVo;
import com.he.eduservice.entity.vo.VideoVo;
import com.he.eduservice.mapper.EduChapterMapper;
import com.he.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testhejinyu
 * @since 2021-06-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService videoService;

    @Override
    public List<ChapterVo> getVideoList(String courseId) {
        List<ChapterVo> result = new ArrayList<>();
        //获取当前课程的所有章节
        QueryWrapper chapterWrapper = new QueryWrapper();
        chapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);

        //获取当前课程中的所有视频
        QueryWrapper chapterVideo = new QueryWrapper();
        chapterWrapper.eq("course_id", courseId);
        List<EduVideo> videoList = videoService.list(chapterVideo);

        //将 视频合并到对应的章节中
        for (int i = 0; i < chapterList.size() ; i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(eduChapter.getId());
            chapterVo.setTitle(eduChapter.getTitle());

            List<VideoVo> chapterVideoList= new ArrayList<>();
            for (int j = 0; j < videoList.size() ; j++) {
                EduVideo eduVideo = videoList.get(i);
                if(eduVideo.getCourseId().equals(chapterVo.getId())){
                    //同一章节的视频
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(eduVideo.getId());
                    videoVo.setFree(eduVideo.getIsFree());
                    chapterVideoList.add(videoVo);
                }
            }

            //将当前章节中的视频添加的章节中
            chapterVo.setChildren(chapterVideoList);
            //将当前章节添加到当前的课程列表中
            result.add(chapterVo);
        }
        return result;
    }

    @Override
    public void addChapter(EduChapter eduChapter) {
        int insert = baseMapper.insert(eduChapter);
        if(insert == 0){
            throw new EduException(20001,"添加章节失败！");
        }
    }
    @Override
    public void updateChapter(EduChapter eduChapter) {
        int update = baseMapper.updateById(eduChapter);
        if(update == 0){
            throw new EduException(20001,"修改章节失败！");
        }
    }

    @Override
    public void removeChapter(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        int delete = baseMapper.delete(queryWrapper);
        if(delete == 0){
            throw new EduException(20001,"删除章节失败！");
        }
    }
}
